<template>
  <div class="mod-config">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="queryList()" style="text-align: right">

      <!--<el-form-item>
        <el-input v-model="searchForm.batchExecLogId" placeholder="批次执行日志ID" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.bizCode" placeholder="业务系统编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.batchCode" placeholder="批次编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.taskCode" placeholder="任务编号" clearable></el-input>
      </el-form-item>-->
      <el-form-item>
        <el-select v-model="searchForm.execStatus" filterable placeholder="请选择运行状态" style="width:100%"
                   @change="queryList()">
          <el-option label="全部" value=""></el-option>
          <template v-for="(item, index) in dict.ExecStatus">
            <el-option :label="item.value +': '+ item.label" :value="item.value"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="自动刷新" style="width:160px">
        <el-select  v-model="searchForm.autoLoad" style="width:80px"
                   @change="startLoop()">
          <el-option label="关闭" value="0"></el-option>
          <el-option label="5秒" value="5"></el-option>
          <el-option label="10秒" value="10"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <!--        <el-button type="primary" @click="queryList(1)">查询</el-button>-->
        <!--        <el-button type="" @click="addEntity()">新增</el-button>-->
        <!--        <el-button type="danger" @click="deleteEntity()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      style="width: 100%;">
      <!--           <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->

      <!--           <el-table-column prop="id"  label="主键" header-align="center" align="center"></el-table-column>-->
      <!--           <el-table-column prop="batchExecLogId"  label="批次执行日志ID" header-align="center" align="center"></el-table-column>-->
      <!--           <el-table-column prop="bizCode"  label="业务系统编号" header-align="center" align="center"></el-table-column>-->
      <!--           <el-table-column prop="batchCode"  label="批次编号" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="taskCode"
                       label="任务编号"
                       header-align="center"
                       align="center"
                       sortable>
      </el-table-column>
      <el-table-column prop="batchTriggerTime" label="批次号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="taskTriggerTime" label="任务号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="dbCode" label="任务数" header-align="center" align="center" width="120"></el-table-column>
      <!--           <el-table-column prop="scheduleType"  label="执行方式" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="execDate" label="执行日期" header-align="center" align="center" width="100"></el-table-column>
      <el-table-column prop="execStatus" label="执行状态" header-align="center" align="center">
        <template slot-scope="scope">
          <template v-for="(item, index) in dict.ExecStatus">
            <el-tag v-if="item.value == scope.row.execStatus" :type="item.style">
              {{item.value}}: {{item.label}}
            </el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="execResult" label="执行结果" header-align="center" align="center"
                       width="100"></el-table-column>
      <!--<el-table-column prop="startupTime" label="启动时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="endTime" label="结束时间" header-align="center" align="center"></el-table-column>-->
      <el-table-column prop="execTime"  label="执行耗时" header-align="center" align="center" width="100">
        <template slot-scope="scope">
          <el-tooltip placement="bottom-end">
            <div slot="content">开始时间：{{scope.row.startupTime}}<br/>结束时间：{{scope.row.endTime}}</div>
            <div>{{scope.row.execTime}}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <!--           <el-table-column prop="createTime"  label="创建时间" header-align="center" align="center"></el-table-column>-->
      <!--           <el-table-column prop="updateTime"  label="更新时间戳" header-align="center" align="center"></el-table-column>-->

      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="160">
        <template slot-scope="scope">
          <!--<el-button type="text" size="small" @click="showDetail(scope.row)">n暂停</el-button>
          <el-button type="text" size="small" @click="showDetail(scope.row)">n续跑</el-button>-->
          <el-button v-if="scope.row.type == 'J'" type="text" size="small" @click="stop(scope.row)">终止</el-button>
          <el-button type="text" size="small" @click="redo(scope.row)">重跑</el-button>
          <el-button v-if="scope.row.type == 'J'" type="text" size="small" @click="showDetail(scope.row)">明细日志</el-button>
          <el-button v-if="scope.row.type != 'J'" type="text" size="small" @click="showDetail(scope.row)">执行器列表</el-button>
          <!--             	<el-button type="text" size="small" @click="modEntity(scope.row)">修改</el-button>-->
          <!--                <el-button type="text" size="small" @click="showDetail(scope.row)">详细</el-button>-->
          <!--                <el-button type="text" size="small" @click="deleteEntity(scope.row.id)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!--    <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>-->
    <!-- 弹窗, 新增 / 修改 -->
    <!--    <add-entity v-if="addEntityVisible" ref="addEntity" @refreshDataList="queryList"></add-entity>
        <mod-entity v-if="modEntityVisible" ref="modEntity" @refreshDataList="queryList"></mod-entity>-->
    <show-detail v-if="showDetailVisible" ref="showDetail"></show-detail>
    <show-list-detail v-if="showListDetailVisible" ref="showListDetail"></show-list-detail>
    <show-list-agent v-if="showListAgentVisible" ref="showListAgent"></show-list-agent>
  </div>
</template>

<script>
  //import AddEntity from './task-exec-log-add'
  //import ModEntity from './task-exec-log-mod'
  import ShowDetail from './job-exec1-log-detail'
  import ShowListDetail from './job-exec1-log-list'
  import ShowListAgent from './task-exec-agent-log-list'
  import {mapState} from "vuex";

  export default {
    data() {
      return {
        timer: null,
        batchExecLog: {},
        isShow: false,
        searchForm: {
          bizCode: '',
          batchCode:'',
          batchExecLogId: '',
          batchTriggerTime: '',
          autoLoad: '10',
        },
        dataList: [],
        page: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addEntityVisible: false,
        modEntityVisible: false,
        showDetailVisible: false,
        showListDetailVisible: false,
        showListAgentVisible: false,
      }
    },
    components: {
      //AddEntity, ModEntity,
      ShowDetail,ShowListDetail,ShowListAgent
    },
    created() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    beforeDestroy() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    activated() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.batchExecLog = null;
      this.batchExecLog = JSON.parse(window.sessionStorage.getItem('__Log_Batch_exec'));
      if(this.batchExecLog){
        this.searchForm.bizCode = this.batchExecLog.bizCode;
        this.searchForm.batchCode = this.batchExecLog.batchCode;
        this.searchForm.batchExecLogId = this.batchExecLog.id;
        this.searchForm.batchTriggerTime = this.batchExecLog.batchTriggerTime;

        this.queryList();
      }
    },
    computed: mapState({
      // listType: state => state.goods.listType
    }),
    mounted() {
      if (this.timer) {
        clearTimeout(this.timer)
      }
    },
    methods: {
      startLoop() {
        let time = parseInt(this.searchForm.autoLoad);
        console.info(time)
        clearTimeout(this.timer)
        this.timer = null;
        if (time < 3) {
          return;
        } else {
          if(this.batchExecLog) {
            //console.info(new Date() + "==>" + parseInt(this.batchExecLog.batchTriggerTime))
            this.searchForm.bizCode = this.batchExecLog.bizCode;
            this.searchForm.batchCode = this.batchExecLog.batchCode;
            this.searchForm.batchExecLogId = this.batchExecLog.id;
            this.searchForm.batchTriggerTime = this.batchExecLog.batchTriggerTime;
            this.timer = setTimeout(this.queryList, time * 1000);
          }
        }
      },
      // 获取数据列表
      queryList(clr) {
        if (typeof (clr) != 'undefined') {
          this.page = 1
        }
        //this.dataListLoading = true
        let obj = {
          'page': this.page,
          'size': 500,
        }
        obj = Object.assign(obj, this.searchForm)
        this.getTableList('/api/batchAllLog/list', obj, (data) => {
          this.dataList = []
          this.dataListLoading = false
          if (data.code == '000000' && data.rows && data.rows.length > 0) {
            this.dataList = data.rows
          }
          this.startLoop();
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.page = 1
        this.queryList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.page = val
        this.queryList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增
      addEntity() {
        this.addEntityVisible = true
        this.$nextTick(() => {
          this.$refs.addEntity.init()
        })
      },
      // 修改
      modEntity(row) {
        let cloneData = Object.assign({}, row)
        this.modEntityVisible = true
        this.$nextTick(() => {
          this.$refs.modEntity.init(cloneData)
        })
      },
      showDetail(row) {
        let cloneData = Object.assign({}, row)
        if (row.type == 'J') {
          this.showDetailVisible = true
          this.$nextTick(() => {
            this.$refs.showDetail.init(cloneData.id, cloneData)
          })
        }else{
          this.showListAgentVisible = true
          this.$nextTick(() => {
            this.$refs.showListAgent.init(cloneData.id, cloneData)
          })
        }

      },
      stop(row) {
        this.$confirm(`确定重跑批量?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let obj = {}
          let uri = '';
          if (row.type == 'J') {
            // 重跑 作业
            obj.jobLogId = row.id;
            uri = '/api/jobExec1Log/stop';
          } else if (row.type == 'T') {
            // 重跑 作业
            obj.taskLogId = row.id;
            uri = '/api/taskExecLog/redo';
          }
          this.getTableList(uri, obj, (data) => {
            this.dataListLoading = false
            if (data && data.code === '000000') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
            } else {
              this.$message.error(data.msg)
            }
            this.queryList();
          })

        });
      },
      redo(row){
        this.$confirm(`确定重跑批量?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let obj = {}
          let uri = '';
          if (row.type == 'J') {
            // 重跑 作业
            obj.jobLogId= row.id;
            uri = '/api/jobExec1Log/redo';
          }else if(row.type == 'T'){
            // 重跑 作业
            obj.taskLogId= row.id;
            uri = '/api/taskExecLog/redo';
          }
          this.getTableList(uri, obj, (data) => {
            this.dataListLoading = false
            if (data && data.code === '000000') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                }
              })
            } else {
              this.$message.error(data.msg)
            }
            this.queryList();
          })

        });

      }
    }
  }
</script>